﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using Gao.Poker.Command;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;
using Gao.Warrior.WPF.Core.Command;

namespace Gao.Poker.App.ViewModel
{
    public class SelectTemplateViewModel : BaseViewModel
    {
        /// <summary>
        /// 传送选中的比赛
        /// </summary>
        private Action<Competition> _action;

        #region Command

        /// <summary>
        /// 搜索
        /// </summary>
        private readonly ICommand _SearchCommand;
        /// <summary>
        /// 确定
        /// </summary>
        private readonly ICommand _SubmitCommand;
        /// <summary>
        /// 关闭
        /// </summary>
        public ICommand CloseCommand { get; set; }
        /// <summary>
        /// 搜索
        /// </summary>
        public ICommand SearchCommand { get { return _SearchCommand; } }
        /// <summary>
        /// 确定
        /// </summary>
        public ICommand SubmitCommand { get { return _SubmitCommand; } }

        #endregion

        private List<Competition> _Competitions;
        public Competition Competition { get; set; }
        public String CompetitionName { get; set; }

        public SelectTemplateViewModel(Action<Competition> action)
        {
            _action = action;
            CompetitionName = String.Empty;
            _SearchCommand = new RelayCommand(OnTemplateSearch);
            CloseCommand = new CloseCommand();
            _SubmitCommand = new RelayCommand(OnSubmitCommand);
        }

        public List<Competition> Competitions
        {
            get
            {
                _Competitions = new CompetitionService().SearchCompetition(CompetitionName);
                return _Competitions;
            }
            set { _Competitions = value; }
        }

       

        private void OnTemplateSearch(object obj)
        {
            OnPropertyChanged("Competitions");
        }

        private void OnSubmitCommand(object obj)
        {
            if (Competition == null)
            {
                ShowMessage("请选择要复制的过往比赛");
                return;
            }

            _action(Competition);

            CloseCommand.Execute(obj);
        }
    }
}
